package com.example.himalaya.view.adapters;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;

import java.util.ArrayList;
import java.util.List;

public class AnnouncerListAdapter extends RecyclerView.Adapter<AnnouncerListAdapter.InnerHolder> {

    // Variable
    private static final String TAG = "AnnouncerListAdapter";

    private List<Announcer> mData = new ArrayList<>();
    private onAnnouncerItemClickListener mItemClickListener = null;
    private onAnnouncerItemLongClickListener mItemLongClickListener = null;


    // 创建显示内容的Item
    @NonNull
    @Override
    public AnnouncerListAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 这里是载view
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_announcer, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull AnnouncerListAdapter.InnerHolder holder, final int position) {
        // 这里是设置数据
        // 将位置下标赋给view的tag
        holder.itemView.setTag(position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick((Integer) view.getTag(), mData.get(position));
                }
                Log.d(TAG, "holder.itemView click ->" + view.getTag());
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                // true 表示消费掉该事件
                if (mItemLongClickListener != null) {
                    int clickPosition = (int) view.getTag();

                    mItemLongClickListener.onItemLongClick(mData.get(clickPosition));
                }

                return true;
            }
        });

        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        // 返回要显示的个数
        if (mData != null) {
            return mData.size();
        }

        return 0;
    }


    // 将从Hemalaya's 的数据放到适配器
    public void setData(List<Announcer> announcerList) {
        if (mData != null) {
            mData.clear();              // 清空
            mData.addAll(announcerList);    // 装入新数据
        }


        // 跟新UI
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(View view) {
            super(view);
        }

        @SuppressLint("DefaultLocale")
        public void setData(Announcer announcer) {
            // 获取对象
            // 封面
            ImageView announcerAvatar = itemView.findViewById(R.id.announcer_avatar_detail);

            // title
            TextView announcerNicename = itemView.findViewById(R.id.announcer_nickname);

            // 描述
            TextView trackCount = itemView.findViewById(R.id.announcer_track_text);

            // 播放数量
            TextView albumCount = itemView.findViewById(R.id.announcer_album_text);


            // 设置数据

            announcerNicename.setText(announcer.getNickname());
            trackCount.setText(String.format("单曲数:%d", announcer.getReleasedTrackCount()));
            albumCount.setText(String.format("专辑数:%d", announcer.getReleasedAlbumCount()));


            String coverUrl = announcer.getAvatarUrl();

            if (!TextUtils.isEmpty(coverUrl)) {
                Picasso.with(itemView.getContext()).load(coverUrl).into(announcerAvatar);
            } else {
                announcerAvatar.setImageResource(R.mipmap.default_picture);
            }

        }
    }

    public void setAnnouncerItemClickListener(onAnnouncerItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public interface onAnnouncerItemClickListener {
        void onItemClick(int position, Announcer announcer);
    }


    public void setAnnouncerItemLongClickListener(onAnnouncerItemLongClickListener listener) {
        this.mItemLongClickListener = listener;
    }

    /**
     * item 长按的接口
     */
    public interface onAnnouncerItemLongClickListener {
        void onItemLongClick(Announcer announcer);
    }
}
